//
//  BkMoneyBackView.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/9.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "BkMoneyBackView.h"
#import "LXOrderMessageValueItemView.h"
#import "LXTitleImgeArrayItemView.h"
#import "LXSaveUserInforTool.h"
#import "BkMessageOrderView.h"
@interface BkMoneyBackView()<UITextViewDelegate>
@property (nonatomic,strong) UIImageView *headerImgView;
@property (nonatomic,strong) UILabel *bkNameLabel;
@property (nonatomic,strong) UILabel *moneyLabel;
@property (nonatomic,strong) UIView *septorLine;
@property (nonatomic,strong) UILabel *amountNameLabel;
@property (nonatomic,strong) UIButton *addBtn;
@property (nonatomic,strong) UIButton *reduceBtn;
@property (nonatomic,strong) UILabel *amountLabel;
@property (nonatomic,strong) LXOrderMessageValueItemView *moneyView;
@property (nonatomic,strong) UILabel *descrpNameLabel;
@property (nonatomic,strong) UITextField  *descrpTextFied;

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic,strong) LXTitleImgeArrayItemView *uploadImgView;

@property (nonatomic,strong) UIButton *ensureBtn;

@property (nonatomic,strong) NSArray *messageArray;
@property (nonatomic,strong) NSMutableArray *uiMessageArray;

@end
@implementation BkMoneyBackView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, TSScreenWidth, TSScaleW(100));
        self.backgroundColor = kSubBacColor;
//        [self addSubview:self.headerImgView];
//        [self addSubview:self.bkNameLabel];
//        [self addSubview:self.moneyLabel];
//        [self addSubview:self.septorLine];
//
//        [self addSubview:self.amountNameLabel];
//        [self addSubview:self.addBtn];
//        [self addSubview:self.amountLabel];
//        [self addSubview:self.reduceBtn];
        [self addSubview:self.moneyView];
        
        [self addSubview:self.descrpNameLabel];
        [self addSubview:self.descrpTextFied];
        
        [self addSubview:self.textView];
        [self addSubview:self.placeHolderLabel];
        
        [self addSubview:self.uploadImgView];
        [self addSubview:self.ensureBtn];
        self.height = self.ensureBtn.bottom + TSScaleW(20);
       // self.messageArray = self.dataArray;
    }
    return self;
}
-(UIImageView *)headerImgView{
    if (!_headerImgView) {
        _headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(TSScaleW(11), TSScaleW(15) , TSScaleW(47), TSScaleW(63))];
        _headerImgView.backgroundColor = kGrayTxtColor;

    }
    return _headerImgView;
}
-(UILabel *)bkNameLabel{
    if (!_bkNameLabel) {
        _bkNameLabel = [TSWLTools allocLabel:@"初中数学运算大法" font:systemFont(TSScaleW(14)) textColor:kMainTxtColor frame:CGRectMake(_headerImgView.right + TSScaleW(10),  _septorLine.bottom + TSScaleW(18), TSScaleW(210), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
        //_bkNameLabel.numberOfLines = 0;
    }
    return _bkNameLabel;
}
-(UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [TSWLTools allocLabel:@"￥999.00" font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(_headerImgView.right + TSScaleW(10), _bkNameLabel.bottom + TSScaleW(20), TSScaleW(260), TSScaleW(15)) textAlignment:(NSTextAlignmentLeft)];
        NSString *string = _moneyLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(1,  string.length - 4)];
        _moneyLabel.attributedText = atts;
    }
    return _moneyLabel;
}
-(UIView *)septorLine{
    if (!_septorLine) {
        _septorLine = [[UIView alloc]initWithFrame:CGRectMake(TSScaleW(11), TSScaleW(26) + _headerImgView.bottom, self.width - TSScaleW(22), .5f)];
        _septorLine.backgroundColor = kMainLineColor;
    }
    return _septorLine;
}
-(UILabel *)amountNameLabel{
    if (!_amountNameLabel) {
        _amountNameLabel = [TSWLTools allocLabel:@"退款数量" font:systemFont(TSScaleW(14)) textColor:kSubTxtColor frame:CGRectMake(TSScaleW(11), TSScaleW(50) + _headerImgView.bottom, TSScaleW(100), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _amountNameLabel;
}
-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [TSWLTools allocButtonTitle:@"+" font:systemBoldFont(18) textColor:kMainTxtColor image:nil frame:CGRectMake(self.width - TSScaleW(25), _amountNameLabel.top, TSScaleW(18), TSScaleW(18)) sel:@selector(addAction:) taget:self];
        [_addBtn setTitleColor:kGrayTxtColor forState:(UIControlStateSelected)];
        _addBtn.hidden = YES;
    }
    return _addBtn;
}
-(UILabel *)amountLabel{
    if (!_amountLabel) {
        _amountLabel = [TSWLTools allocLabel:@"1" font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(0, 0, TSScaleW(33), TSScaleW(17)) textAlignment:(NSTextAlignmentCenter)];
        _amountLabel.backgroundColor = kMainBgColor;
        _amountLabel.centerY = _addBtn.centerY;
        _amountLabel.right = _addBtn.left;
        _amountLabel.hidden = YES;
    }
    return _amountLabel;
}
-(UIButton *)reduceBtn{
    if (!_reduceBtn) {
        _reduceBtn = [TSWLTools allocButtonTitle:@"-" font:systemBoldFont(18) textColor:kMainTxtColor image:nil frame:CGRectMake(0, _addBtn.top, TSScaleW(18), TSScaleW(18)) sel:@selector(reduceAction:) taget:self];
        [_reduceBtn setTitleColor:kGrayTxtColor forState:(UIControlStateSelected)];
        _reduceBtn.right = _amountLabel.left;
        _reduceBtn.hidden = YES;
    }
    return _reduceBtn;
}
-(void)addAction:(UIButton *)sender{
    self.amountCount ++;
  
}
-(void)reduceAction:(UIButton *)sender
{
    if (self.amountCount<= 1) {
        self.amountCount = 1;
    }else{
        self.amountCount--;
    }
}
-(void)setAmountCount:(NSInteger)amountCount
{
    _amountCount = amountCount;
    
    _amountLabel.text = [NSString stringWithFormat:@"%ld",_amountCount];
//    if (_detailModel) {
//        double index  =  _detailModel.goodsprice1.floatValue *_amountCount;
//        _totalLabel.text = [NSString stringWithFormat:@"总计：￥%.2f",index];
//        _scoreLabel.text = [NSString stringWithFormat:@"%ld积分",_detailModel.goodsprice.integerValue *_amountCount];
//        !self.scoreAmountBlock?:self.scoreAmountBlock(_amountCount);
//    }
    
}
- (LXOrderMessageValueItemView *)moneyView{
    if (!_moneyView) {
        _moneyView = [[LXOrderMessageValueItemView alloc]initWithTop:TSScaleW(23) + _amountNameLabel.bottom titleString:@"退款金额" valueString:@"￥0.00" isRedValue:NO];
        _moneyView.titleLabel.textColor = kGrayTxtColor;
        _moneyView.titleLabel.font = systemFont(TSScaleW(12));
        _moneyView.valueLabel.textColor = kRedColor;
        _moneyView.valueLabel.font = systemFont(TSScaleW(12));
        NSString *string = _moneyView.valueLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(1,  string.length - 4)];
        _moneyView.valueLabel.attributedText = atts;
        _moneyView.backgroundColor = kSubBacColor;
        _moneyView.width = self.width;
        _moneyView.valueLabel.right = self.width - TSScaleW(11);
    }
    return _moneyView;
}
-(UILabel *)descrpNameLabel{
    if (!_descrpNameLabel) {
        _descrpNameLabel = [TSWLTools allocLabel:@"退款说明" font:systemFont(TSScaleW(14)) textColor:kSubTxtColor frame:CGRectMake(TSScaleW(11), _moneyView.bottom + TSScaleW(20), TSScaleW(100), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _descrpNameLabel;
}
-(UITextField *)descrpTextFied{
    if (!_descrpTextFied) {
        _descrpTextFied = [TSWLTools allocTextFieldText:systemFont(TSScaleW(12)) placeHolderFont:systemFont(TSScaleW(12)) text:nil placeText:@"选填" textColor:kMainTxtColor placeHolderTextColor:kGrayTxtColor frame:CGRectMake(0, _descrpNameLabel.top, TSScaleW(200), _descrpNameLabel.height)];
        _descrpTextFied.right = self.width - TSScaleW(11);
        _descrpTextFied.textAlignment = NSTextAlignmentRight;
    }
    return _descrpTextFied;
}
-(UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(TSScaleW(20), TSScaleW(10) + self.descrpTextFied.bottom, self.width - TSScaleW(40), TSScaleW(70))];
        _textView.delegate = self;
        _textView.font = kFont(15);
        _textView.backgroundColor = kMainBgColor;
    }
    return _textView;
}
-(UILabel *)placeHolderLabel{
    if (!_placeHolderLabel) {
        _placeHolderLabel = [TSWLTools allocLabel:@"请输入您的退款原因" font:systemFont(14) textColor:kGrayTxtColor frame:CGRectMake(_textView.left, _textView.top + TSScaleW(5), _textView.width - TSScaleW(20), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _placeHolderLabel;
}
#pragma mark ------textViewDelegate

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    self.placeHolderLabel.hidden = YES;
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *contentString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if (contentString.length == 0) {
        self.placeHolderLabel.hidden = NO;
    }else{
        self.placeHolderLabel.hidden = YES;
    }
   
    
    return YES;
}
-(LXTitleImgeArrayItemView *)uploadImgView{
    if (!_uploadImgView) {
        [LXSaveUserInforTool sharedUserTool].medaiType = 1;
        _uploadImgView = [[LXTitleImgeArrayItemView alloc]initWithTop:_textView.bottom +TSScaleW(20) title:@"上传凭证" imgeArray:@[@""] redHidden:YES];
       
        _uploadImgView.limitCount = 4;
        _uploadImgView.backgroundColor = kSubBacColor;
        
    }
    return _uploadImgView;
}
-(NSArray *)urlsString{
    
    return _uploadImgView.urlsArray;
}
-(NSString *)refundreason{
    return _textView.text.length?_textView.text:@"";
}
-(NSString *)refunddesc
{
    return _descrpTextFied.text.length?_descrpTextFied.text:@"";
}
-(UIButton *)ensureBtn
{
    if (!_ensureBtn) {
        _ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ensureBtn.frame = CGRectMake(TSScaleW(11), _uploadImgView.bottom + TSScaleW(70), self.width - TSScaleW(22),TSScaleW(40));
        [_ensureBtn btn:_ensureBtn font:TSScaleW(14) textColor:kWhiteColor text:@"提交" image:nil sel:@selector(ensureBtnAction:) taget:self];
        _ensureBtn.backgroundColor = kGreenColor;
        _ensureBtn.cornerRadius = _ensureBtn.height/2.f;
    }
    return _ensureBtn;
}
-(void)ensureBtnAction:(UIButton *)sender
{
    !self.callBackBlock?:self.callBackBlock();
}

-(void)setMessageArray:(NSArray *)messageArray{
    _messageArray = messageArray;
    for (UIView *v  in self.uiMessageArray) {
        [v removeFromSuperview];
    }
    [self.uiMessageArray removeAllObjects];
    CGFloat top = TSScaleW(20);
    for (int i = 0; i <self.messageArray.count; i ++) {
        NSDictionary *dataDic = self.messageArray[i];
        BkMessageOrderView *messageView = [[BkMessageOrderView alloc]init];
        messageView.top = top;
        top += messageView.height;
        _moneyView.top =  TSScaleW(23) + messageView.bottom;
        messageView.dataDic = dataDic;
        [self addSubview:messageView];
        [self.uiMessageArray addObject:messageView];
    }
    _descrpNameLabel.top = _moneyView.bottom + TSScaleW(20);
    _descrpTextFied.top = _descrpNameLabel.top;
    _textView.top = TSScaleW(10) + _descrpTextFied.bottom;
    _placeHolderLabel.top = _textView.top + TSScaleW(5);
    _uploadImgView.top = _textView.bottom +TSScaleW(20);
    [_uploadImgView removeFromSuperview];
    _uploadImgView = nil;
    [self addSubview:self.uploadImgView];
    _ensureBtn.top = _uploadImgView.bottom + TSScaleW(70);
    self.height = self.ensureBtn.bottom + TSScaleW(20);
   // self.totalAmountLabel.text = [NSString stringWithFormat:@"@"共1件商品""]
    
    
}
-(NSMutableArray *)uiMessageArray{
    if (!_uiMessageArray) {
        _uiMessageArray = [NSMutableArray array];
    }
    return _uiMessageArray;
}
-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.messageArray = _dataArray;
}
-(void)setMoney:(double)money{
    _money = money;
    self.moneyView.valueLabel.text = [NSString stringWithFormat:@"￥%.2f",_money];
}
@end
